import django_filters
from django import forms
from .models import AlarmRecord
from subscriptions.models import DataSource


class AlarmRecordFilter(django_filters.FilterSet):
    """报警记录过滤器"""
    created_at_after = django_filters.DateTimeFilter(
        field_name='created_at',
        lookup_expr='gte',
        label='开始时间',
        widget=forms.DateTimeInput(attrs={
            'class': 'form-control',
            'type': 'datetime-local'
        })
    )
    created_at_before = django_filters.DateTimeFilter(
        field_name='created_at',
        lookup_expr='lte',
        label='结束时间',
        widget=forms.DateTimeInput(attrs={
            'class': 'form-control',
            'type': 'datetime-local'
        })
    )
    subscription_type = django_filters.ChoiceFilter(
        choices=AlarmRecord.SUBSCRIPTION_TYPES,
        label='订阅类型',
        empty_label='全部类型',
        widget=forms.Select(attrs={
            'class': 'form-select'
        })
    )
    data_source = django_filters.ModelChoiceFilter(
        queryset=DataSource.objects.all(),
        label='数据源',
        empty_label='全部数据源',
        widget=forms.Select(attrs={
            'class': 'form-select',
        })
    )
    acknowledged = django_filters.BooleanFilter(
        label='确认状态',
        widget=forms.Select(attrs={
            'class': 'form-select'
        }, choices=[
            ('', '全部状态'),
            (True, '已确认'),
            (False, '未确认')
        ])
    )
    name = django_filters.CharFilter(
        lookup_expr='icontains',
        label='数据名称',
        widget=forms.TextInput(attrs={
            'class': 'form-control',
            'placeholder': '请输入数据名称'
        })
    )
    identifier = django_filters.CharFilter(
        method='filter_identifier',
        label='标识符',
        widget=forms.TextInput(attrs={
            'class': 'form-control',
            'placeholder': '请输入标识符'
        })
    )
    values_id = django_filters.CharFilter(
        method='filter_values_id',
        label='监控项',
        widget=forms.TextInput(attrs={
            'class': 'form-control',
            'placeholder': '请输入监控项'
        })
    )

    class Meta:
        model = AlarmRecord
        fields = [
            'created_at_after',
            'created_at_before',
            'subscription_type',
            'data_source',
            'acknowledged',
            'name',
            'identifier',
            'values_id'
        ]

    def filter_identifier(self, queryset, name, value):
        """过滤标识符
        
        对 OPC UA 的 node_id 和 MQTT 的 topic 进行过滤
        """
        if value:
            return queryset.filter(
                opcua_subscription__node_id__icontains=value
            ) | queryset.filter(
                mqtt_subscription__topic__icontains=value
            )
        return queryset

    def filter_values_id(self, queryset, name, value):
        """过滤监控项
        
        对 MQTT 监控项的 values_id 进行过滤
        """
        if value:
            return queryset.filter(mqtt_item__values_id__icontains=value)
        return queryset
